<?php

if (isset($_GET['action']) && $_GET['action'] == "remove" && is_allowed($USER_RIGHTS['raid_delete']))  {

	$raid_id = $_GET['raid_id'];

	$raid->remove($raid_id);

	$smarty->assign('message', "The raid has been removed!");

}

$current_raids = $raid->get();

$previous_raids = $raid->get(0,1,35);


$rp_categories = $rp_category->get();
$smarty->assign('rp_categories', $rp_categories);

// ACTION: NONE, SHOW ALL RAIDS
foreach ($current_raids as $id => $cur_raid) {
	$main_signups = $signup->count($cur_raid['raid_id'], 0, 0);
	$main_subs = $signup->count($cur_raid['raid_id'], 0, 1);

	$current_raids[$id]['signups'] = $main_signups[0]['sign_ups'];
	$current_raids[$id]['subs'] = $main_subs[0]['sign_ups'];

	$player_signups = $signup->get($cur_raid['raid_id'], 0, 0, $_SESSION['uid']);

	//user_status values
	//1: user is signed up as a main
	//2: user is signed up as a sub
	//3: user is NOT signed up and can sign up as main
	//4: user is NOT signed up and can sign up as sub
	//5: user is NOT signed up and cannot sign up at all
	//6: raid is frozen, so the player cant sign up!


//	$start_time = strtotime(str_replace("-","/",$cur_raid['date_time']));
//
//	$freeze_time = $cur_raid['freeze_time'] * 60 * 60;
//
//
//	if(($start_time - $freeze_time) < time()) {
//		$current_raids[$id]['user_status'] = "6"; //raid is frozen, so the player cant sign up!
//	} else {

		if (count($player_signups) > 0) {
			$current_raids[$id]['user_status'] = "1"; //user is signed up as a main
		} else{




			$player_subs = $signup->get($cur_raid['raid_id'], 0, 1, $_SESSION['uid']);

			if (count($player_subs) > 0) {
				$current_raids[$id]['user_status'] = "2";
			} else {

				$player_active = $player->get(0,"", 1, $_SESSION['uid']);

				if (count($player_active) > 0) {

					if ($player_active[0]['team_order'] >= $current_raids[$id]['main_team_order']) {
						$current_raids[$id]['user_status'] = "3";
					} elseif ($player_active[0]['team_order'] >= $current_raids[$id]['sub_team_order']) {
						$current_raids[$id]['user_status'] = "4";
					} elseif ($player_active[0]['team_order'] < $current_raids[$id]['sub_team_order']) {
						$current_raids[$id]['user_status'] = "5";
					}



				} else {
					$current_raids[$id]['user_status'] = "5";
				}
			}



		}

//	}
}


foreach ($previous_raids as $id => $prev_raid) {
	$main_signups = $signup->count($prev_raid['raid_id'], 0, 0);
	$main_subs = $signup->count($prev_raid['raid_id'], 0, 1);

	$previous_raids[$id]['signups'] = $main_signups[0]['sign_ups'];
	$previous_raids[$id]['subs'] = $main_subs[0]['sign_ups'];

}



//echo "<pre>";
//print_r($current_raids);
//echo "</pre>";

// ACTION: NEW RAID
if (isset($_GET['action']) && $_GET['action'] == "new") {



	$team = new team();

	$teams = $team->getOption();

	$warriors[0] = "- None -";
	
	$players = $player->getOption("", 5);

	$events = $raid->event();

	$class_roles = $class_role->get();
	
	$roles = $class_role->getRoles();
	
	foreach($roles as $key  => $role_item)
	{
		foreach ($class_roles as $class_role_item) 
		{
			if($class_role_item['role_id'] == $role_item['id'])
			{		
				
				$roles[$key]['class_roles'][] = $class_role_item;
				
			}
		}			
	}
	
	$smarty->assign('roles', $roles);
	
	$smarty->assign('events', $events);

	$smarty->assign('players', $players);

	$smarty->assign('warriors', $warriors);

	$smarty->assign('teams', $teams);

	$smarty->display('raid_new.tpl', $_SESSION['uid']);

// ACTION: EDIT RAID
} elseif (isset($_GET['action']) && $_GET['action'] == "edit" && is_allowed($USER_RIGHTS['raid_change'])) {

	$raid_id = $_GET['raid_id'];

	$team = new team();

	$teams = $team->getOption();

	$players = $player->getOption();

	$warriors[0] = "- None -";
	
	//$warriors_all = $player->getOption("Warrior");

	//$warriors = array_merge($warriors, $warriors_all);

	$events = $raid->event();

	$edit_raid = $raid->get($raid_id);

	$class_roles = $class_role->get();
	
	$roles = $class_role->getRoles();
	
	
	
	foreach($roles as $key  => $role_item)
	{
		foreach ($class_roles as $class_role_item) 
		{
			if($class_role_item['role_id'] == $role_item['id'])
			{		
				
				$roles[$key]['class_roles'][] = $class_role_item;
				
			}
		}			
	}
	
	$raid_roles = $raid_role->get($raid_id);
	
	foreach($raid_roles as $key  => $raid_role_item)
	{
		if(!empty($raid_role_item['role_name']))
		{
			$assigned_raid_roles[$raid_role_item['role_name']] = $raid_role_item['max_main'];
			
		}elseif(!empty($raid_role_item['class_role_name']))
		{
			$assigned_raid_roles[$raid_role_item['class_role_name']]['value'] = $raid_role_item['max_main'];
			$assigned_raid_roles[$raid_role_item['class_role_name']]['id'] = $raid_role_item['id'];
			
		}
		
	}
	
	$smarty->assign('assigned_raid_roles',$assigned_raid_roles);
	
	$smarty->assign('roles', $roles);
	
	$smarty->assign('events', $events);

	$smarty->assign('edit_raid', $edit_raid[0]);

	$smarty->assign('players', $players);

	$smarty->assign('warriors', $warriors);

	$smarty->assign('teams', $teams);

	$smarty->display('raid_edit.tpl', $_SESSION['uid']);

// ACTION: ADD RAID TO DB
} elseif (isset($_POST['action']) && $_POST['action'] == "add" && is_allowed($USER_RIGHTS['raid_add'])) {

	//we are gonna check all the values, 1 by 1 then we send them back to the raids.tpl page
	// first up, event_id
	// NAME: 				event
	// Allowed values: 	Numeric, anything bigger then 0
	// Empty?:			No

	$validated = 1;
	$event_id		=	0;
	$leader_id		=	0;
	$maintank1_id	=	0;
	$maintank2_id	=	0;
	$mainlimit		=	0;
	$sublimit		=	0;
	$mainteam		=	0;
	$subteam		=	0;
	$date			=	"";
	$comment		=	"";
	$class_max		=	0;

	$class_max = abs($_POST['class_max']);

	if (!empty($_POST['event']))
	{
		$event_id = abs($_POST['event']);

		if (!($event_id > 0))
		{
			$smarty->assign('event_error', "the event id that was selected is not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('event_error', "please select a event.");
		$validated = 0;
	}


	$maintank1_id = abs($_POST['mt1']);

	if (!($maintank1_id > -1))
	{
		$smarty->assign('mt1_error', "the selected Maintank 1 is not correct.");
		$validated = 0;
	}

	$maintank2_id = abs($_POST['mt2']);

	if (!($maintank2_id > -1))
	{
		$smarty->assign('mt2_error', "the selected Maintank 1 is not correct.");
		$validated = 0;
	}


	if (!empty($_POST['leader']))
	{
		$leader_id = abs($_POST['leader']);

		if (!($leader_id > 0))
		{
			$smarty->assign('leader_error', "the leader that was selected is not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('leader_error', "please select a leader.");
		$validated = 0;
	}

	if (!empty($_POST['mainlimit']))
	{
		$mainlimit = abs($_POST['mainlimit']);

		if (!($mainlimit > 0))
		{
			$smarty->assign('mainlimit_error', "the limit selected for the Main's signup was too low.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('mainlimit_error', "please enter an Main signup limit.");
		$validated = 0;
	}


	if (!empty($_POST['sublimit']))
	{
		$sublimit = abs($_POST['sublimit']);

		if (!($sublimit > 0))
		{
			$smarty->assign('sublimit_error', "the limit selected for the Sub's signup was too low.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('sublimit_error', "please enter an Sub signup limit.");
		$validated = 0;
	}

	if (!empty($_POST['mainteam']))
	{
		$max_main_team = abs($_POST['mainteam']);

		if (!($max_main_team > 0))
		{
			$smarty->assign('mainteam_error', "the selected team for the Main signups was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('mainteam_error', "please select an Main signup team.");
		$validated = 0;
	}


	if (!empty($_POST['subteam']))
	{
		$max_sub_team = abs($_POST['subteam']);

		if (!($max_sub_team > 0))
		{
			$smarty->assign('subteam_error', "the selected team for the Sub signups was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('subteam_error', "please select an Sub signup team.");
		$validated = 0;
	}

	if (!empty($_POST['freeze_time']))
	{
		$freeze_time = abs($_POST['freeze_time']);

		if (!($freeze_time >= 0))
		{
			$smarty->assign('freeze_time_error', "the freeze time was not correct. Please use positive numbers only!");
			$validated = 0;
		}

	}
	else
	{
		$freeze_time = 0;
	}

	if (!empty($_POST['date']))
	{
		$date = $_POST['date'];

	}
	else
	{
		$smarty->assign('date_error', "please select an correct date.");
		$validated = 0;
	}

	if (!empty($_POST['rp_category_id']))
	{
		$rp_category_id = abs($_POST['rp_category_id']);

		if (!($rp_category_id > 0))
		{
			$smarty->assign('rp_category_id_error', "the selected RP category was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('rp_category_id_error', "please select an RP category.");
		$validated = 0;
	}

	$comment = 		$_POST['comment'];

	if ($validated == 0) {

		$team = new team();

		$teams = $team->getOption();

		$players = $player->getOption();

		$warriors = $player->getOption("Warrior");

		$events = $raid->event();


		$smarty->assign('event_id', $event_id);
		$smarty->assign('leader_id', $leader_id);
		$smarty->assign('maintank1_id', $maintank1_id);
		$smarty->assign('maintank2_id', $maintank2_id);
		$smarty->assign('mainlimit', $mainlimit);
		$smarty->assign('sublimit', $sublimit);
		$smarty->assign('mainteam', $mainteam);
		$smarty->assign('subteam', $subteam);
		$smarty->assign('date', $date);
		$smarty->assign('comment', $comment);
		$smarty->assign('freeze_time', $feeze_time);


		$smarty->assign('events', $events);

		$smarty->assign('players', $players);

		$smarty->assign('warriors', $warriors);

		$smarty->assign('teams', $teams);

		$smarty->display('raid_new.tpl', $_SESSION['uid']);

	} else {

		$new_raid_id = $raid->save(0, $date, $mainlimit, $sublimit, htmlspecialchars($comment), $leader_id, $maintank1_id, $maintank2_id, $event_id, 0, $max_main_team, $max_sub_team, $rp_category_id, $freeze_time);

		$class_roles = $class_role->get();
		
		$roles = $class_role->getRoles();
		
		foreach($roles as $key  => $role_item)
		{
			$class_role_name = str_replace(" ", "_" , $role_item['role_name']);
			
			
			if($_POST[$class_role_name] > 0)
			{

				$raid_role->save(0, $new_raid_id, 0, $role_item['id'], $_POST[$class_role_name]);
				
			}
		}

		foreach ($class_roles as $class_role_item) 
		{
			$class_role_name = str_replace(" ", "_" , $class_role_item['class_role_name']);
			
			if($_POST[$class_role_name] > 0)
			{
				
				$raid_role->save(0, $new_raid_id, $class_role_item['id'], 0, $_POST[$class_role_name]);
				
			}
		}	
		
		$smarty->assign('message', "The new raid has been added to the system.<br>Click on <a href=\"./index.php?page=raids\">Raids</a> to return to the raid page.");

		$smarty->display('raid_success.tpl', $_SESSION['uid']);

	}

// ACTION: SAVE RAID TO DB
} elseif (isset($_POST['action']) && $_POST['action'] == "save" && (is_allowed($USER_RIGHTS['raid_add']) || is_allowed($USER_RIGHTS['raid_change']))) {

	//we are gonna check all the values, 1 by 1 then we send them back to the raids.tpl page
	// first up, event_id
	// NAME: 				event
	// Allowed values: 	Numeric, anything bigger then 0
	// Empty?:			No

	$validated = 1;
	$event_id		=	0;
	$leader_id		=	0;
	$maintank1_id	=	0;
	$maintank2_id	=	0;
	$mainlimit		=	0;
	$sublimit		=	0;
	$mainteam		=	0;
	$subteam		=	0;
	$date			=	"";
	$comment		=	"";
	$raid_id		= $_POST['raid_id'];
	$class_max = abs($_POST['class_max']);

	if (!empty($_POST['event']))
	{
		$event_id = abs($_POST['event']);

		if (!($event_id > 0))
		{
			$smarty->assign('event_error', "the event id that was selected is not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('event_error', "please select a event.");
		$validated = 0;
	}


	$maintank1_id = abs($_POST['mt1']);

	if (!($maintank1_id > -1))
	{
		$smarty->assign('mt1_error', "the selected Maintank 1 is not correct.");
		$validated = 0;
	}

	$maintank2_id = abs($_POST['mt2']);

	if (!($maintank2_id > -1))
	{
		$smarty->assign('mt2_error', "the selected Maintank 1 is not correct.");
		$validated = 0;
	}


	if (!empty($_POST['leader']))
	{
		$leader_id = abs($_POST['leader']);

		if (!($leader_id > 0))
		{
			$smarty->assign('leader_error', "the leader that was selected is not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('leader_error', "please select a leader.");
		$validated = 0;
	}

	if (!empty($_POST['mainlimit']))
	{
		$mainlimit = abs($_POST['mainlimit']);

		if (!($mainlimit > 0))
		{
			$smarty->assign('mainlimit_error', "the limit selected for the Main's signup was too low.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('mainlimit_error', "please enter an Main signup limit.");
		$validated = 0;
	}


	if (!empty($_POST['sublimit']))
	{
		$sublimit = abs($_POST['sublimit']);

		if (!($sublimit > 0))
		{
			$smarty->assign('sublimit_error', "the limit selected for the Sub's signup was too low.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('sublimit_error', "please enter an Sub signup limit.");
		$validated = 0;
	}

	if (!empty($_POST['mainteam']))
	{
		$max_main_team = abs($_POST['mainteam']);

		if (!($max_main_team > 0))
		{
			$smarty->assign('mainteam_error', "the selected team for the Main signups was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('mainteam_error', "please select an Main signup team.");
		$validated = 0;
	}


	if (!empty($_POST['subteam']))
	{
		$max_sub_team = abs($_POST['subteam']);

		if (!($max_sub_team > 0))
		{
			$smarty->assign('subteam_error', "the selected team for the Sub signups was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('subteam_error', "please select an Sub signup team.");
		$validated = 0;
	}

	if (!empty($_POST['rp_category_id']))
	{
		$rp_category_id = abs($_POST['rp_category_id']);

		if (!($rp_category_id > 0))
		{
			$smarty->assign('rp_category_id_error', "the selected RP category was not correct.");
			$validated = 0;
		}

	}
	else
	{
		$smarty->assign('rp_category_id_error', "please select an RP category.");
		$validated = 0;
	}


	if (!empty($_POST['date']))
	{
		$date = $_POST['date'];

	}
	else
	{
		$smarty->assign('date_error', "please select an correct date.");
		$validated = 0;
	}

	if (!empty($_POST['freeze_time']))
	{
		$freeze_time = abs($_POST['freeze_time']);

		if (!($freeze_time >= 0))
		{
			$smarty->assign('freeze_time_error', "the freeze time was not correct. Please use positive numbers only!");
			$validated = 0;
		}

	}
	else
	{
		$freeze_time = 0;
	}

	$comment = $_POST['comment'];

	if ($validated == 0) {

		$team = new team();


		$teams = $team->getOption();

		$players = $player->getOption();

		$warriors = $player->getOption("Warrior");

		$events = $raid->event();

		$edit_raid = $raid->get($_POST['raid_id']);

		$smarty->assign('events', $events);

		$smarty->assign('edit_raid', $edit_raid[0]);

		$smarty->assign('players', $players);

		$smarty->assign('warriors', $warriors);

		$smarty->assign('teams', $teams);

		$smarty->display('raid_edit.tpl', $_SESSION['uid']);

	} else {

		$raid->save($raid_id, $date, $mainlimit, $sublimit, htmlspecialchars($comment), $leader_id, $maintank1_id, $maintank2_id, $event_id, 0, $max_main_team, $max_sub_team, $rp_category_id, $freeze_time);

		$raid_roles = $raid_role->get($raid_id);
		 
		foreach($raid_roles as $key  => $raid_role_item)
		{
		
			if($_POST[$raid_role_item['id']] > 0)
			{
			
				$raid_role->save($raid_role_item['id'], 0, 0, 0, $_POST[$raid_role_item['id']]);
			
			}
			
		}
		
		$smarty->assign('message', "The raid has been updated with the new information.<br>Click on <a href=\"./index.php?page=raids\">Raids</a> to return to the raid page.");

		$smarty->display('raid_success.tpl', $_SESSION['uid']);

	}



} else {

	
	foreach ($current_raids as $key => $current_item) 
	{
		$raid_roles = $raid_role->get($current_item['raid_id']);
		
		$current_raids[$key]['raid_roles'] = $raid_roles;
		
	}

	foreach ($previous_raids as $key  => $previous_item) 
	{
		
		$raid_roles = $raid_role->get($previous_item['raid_id']);
		
		$previous_raids[$key]['raid_roles'] = $raid_roles;
		
	}

	$smarty->assign('previous_raids', $previous_raids);

	$smarty->assign('current_raids', $current_raids);
	
	$smarty->display('raids.tpl', $_SESSION['uid']);

}

?>